package vsvm.gui.forms;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JTabbedPane;
import javax.swing.border.EtchedBorder;

import vsvm.classifier.AbstractTestSet;
import vsvm.gui.Environment;
import vsvm.gui.VSVMInternalFrame;
import vsvm.gui.controls.LayoutableDesktopPane;
import vsvm.gui.controls.QuickInfoPanel;
import vsvm.gui.controls.StatusBar;
import vsvm.gui.controls.TaskBar;
import vsvm.gui.controls.TestSetList;
import vsvm.gui.controls.WorkspaceTree;
import vsvm.gui.events.WorkspaceChangedListener;
import vsvm.gui.menus.MainMenu;
import vsvm.gui.objects.Workspace;
import javax.swing.JToolBar;


public class MainFrame extends JFrame implements WorkspaceChangedListener {

	private MainMenu menu = null;
	private StatusBar statusBar = null;
	private JPanel framePanel = null;
	private JSplitPane splitPane = null;
	private JSplitPane workspacePanel = null;
	private LayoutableDesktopPane desktopPane = null;
	private JTabbedPane workspaceTabs = null;
	private QuickInfoPanel qInfoPanel = null;
	private WorkspaceTree workspaceTree = null;
	private TestSetList testSetList = null;
	private JPanel desktopPanel = null;
	
	
	private Workspace workspace = null;
	private TaskBar jWindowsBarScrollPane = null;
	/**
	 * This method initializes 
	 * 
	 */
	public MainFrame() {
		super();
		initialize();
		
		Environment.getEnvironment().setMainFrame(this);
		
		update();
		
		this.addWindowListener(new WindowAdapter(){
			
			public void windowClosing(WindowEvent e)
			{
				if (Environment.getEnvironment().getWorkspace() == null) return;	
				
				if (JOptionPane.showConfirmDialog(MainFrame.this, "Do you want to save changes to current workspace?", "VSVM", JOptionPane.YES_NO_OPTION) == JOptionPane.OK_OPTION)
					Environment.getEnvironment().getWorkspace().save();				
			}
			
		});
	}
	
	public void update() {
		menu.update();
		if (Environment.getEnvironment().getWorkspace() == null) {
			workspacePanel.setEnabled(false);
			workspaceTabs.setEnabled(false);
			workspaceTree.setEnabled(false);
		} else {
			workspacePanel.setEnabled(true);
			workspaceTabs.setEnabled(true);
			workspaceTree.setEnabled(true);
		}
	}

	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {
        this.setSize(new java.awt.Dimension(640,480));
        this.setTitle("Visual MultiSVM");
        this.setJMenuBar(getMainMenu());
        this.setContentPane(getFramePanel());
        this.setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
	}

	/**
	 * This method initializes framePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JPanel getFramePanel() {
		if (framePanel == null) {
			framePanel = new JPanel();
			framePanel.setLayout(new BorderLayout());
			framePanel.add(getStatusBar(), BorderLayout.SOUTH);
			
			splitPane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, getWorkspacePanel(), getDesktopPanel());
			framePanel.add(splitPane, java.awt.BorderLayout.CENTER);
		}
		return framePanel;
	}

	/**
	 * This method initializes workspacePanel	
	 * 	
	 * @return javax.swing.JPanel	
	 */
	private JSplitPane getWorkspacePanel() {
		if (workspacePanel == null) {
			workspacePanel = new JSplitPane(JSplitPane.VERTICAL_SPLIT, getWorkspaceTabs(), getQInfoPanel());
			workspacePanel.setPreferredSize(new java.awt.Dimension(200,100));
			workspacePanel.setBorder(new EtchedBorder());
			workspacePanel.setDividerLocation(250);
			workspacePanel.setResizeWeight(1);
		}
		return workspacePanel;
	}

	/**
	 * This method initializes jDesktopPane	
	 * 	
	 * @return javax.swing.JDesktopPane	
	 */
	private LayoutableDesktopPane getDesktopPane() {
		if (desktopPane == null) {
			desktopPane = new LayoutableDesktopPane();
		}
		return desktopPane;
	}
	
	private JPanel getDesktopPanel() {
		if (desktopPanel == null) {
			desktopPanel = new JPanel(new BorderLayout());
			desktopPanel.add(getDesktopPane(), BorderLayout.CENTER);
			desktopPanel.add(getJWindowsBarScrollPane(), java.awt.BorderLayout.SOUTH);
		}
		return desktopPanel; 
	}
	
	
	private MainMenu getMainMenu() {
		if (menu == null) {
			menu = new MainMenu();
		}
		
		return menu;
	}
	
	
	private StatusBar getStatusBar() {
		if (statusBar == null) {
			statusBar = new StatusBar();
		}
		
		return statusBar;
	}
	
	private JTabbedPane getWorkspaceTabs() {
		if (workspaceTabs == null) {
			workspaceTabs = new JTabbedPane();
			JScrollPane scrollPane = new JScrollPane(getWorkspaceTree());
			workspaceTabs.addTab("Workspace", scrollPane);
			workspaceTabs.addTab("Testsets", getTestSets());
		}
		
		return workspaceTabs;
	}
	
	private QuickInfoPanel getQInfoPanel() {
		if (qInfoPanel == null) {
			qInfoPanel = new QuickInfoPanel();
			qInfoPanel.setPreferredSize(new java.awt.Dimension(200,150));
		}
		
		return qInfoPanel;
	}
	
	private WorkspaceTree getWorkspaceTree() {
		if (workspaceTree == null) {
			workspaceTree = new WorkspaceTree(workspace, getQInfoPanel());
		}
		
		return workspaceTree;
	}
	
	
	private JScrollPane getTestSets() {
		if (testSetList == null) {
			testSetList = new TestSetList();
		}
		
		return new JScrollPane(testSetList);		
	}

	
	
	
	public void setTBarVisible(boolean vis) {		
	}
	
	
	public void setSBarVisible(boolean vis) {
		statusBar.setVisible(vis);		
	}
	
	public boolean isTBarVisible() {
		return false;
	}
	
	
	public boolean isSBarVisible() {
		return statusBar.isVisible();		
	}
	
	/**
	 * This method initializes jWindowsBarScrollPane	
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	private TaskBar getJWindowsBarScrollPane() {
		if (jWindowsBarScrollPane == null) {
			jWindowsBarScrollPane = new TaskBar();
			//jWindowsBarScrollPane.setVerticalScrollBarPolicy(javax.swing.JScrollPane.VERTICAL_SCROLLBAR_NEVER);
			//jWindowsBarScrollPane.setPreferredSize(new java.awt.Dimension(200, 30));
			
			//JButton button = new JButton("tst");
			
			//button.setPreferredSize(new java.awt.Dimension(50, 20));
			//button.setVisible(true);
			//JPanel bPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
			
			
			//jWindowsBarScrollPane.getViewport().setPreferredSize(new Dimension(20, 50));
			//jWindowsBarScrollPane.getViewport().add(bPanel);
			//this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			//this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			//this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			/*this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));
			this.jWindowsBarScrollPane.addWindow(new JInternalFrame("tstasdfsa"));*/
			Environment.getEnvironment().setTeskBar(this.jWindowsBarScrollPane);
			
			
			
		}
		return jWindowsBarScrollPane;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		MainFrame frm = new MainFrame();
		frm.setVisible(true);

	}

	public void workspaceChanged() {
		update();
		workspaceTree.reload();
		reloadTestSets();
	}
	
	
	private void reloadTestSets() {
		DefaultListModel listModel = new DefaultListModel();
		if (Environment.getEnvironment().getWorkspace() != null &&
				Environment.getEnvironment().getWorkspace().getTestSets() != null) {
			ArrayList<AbstractTestSet> ts = Environment.getEnvironment().getWorkspace().getTestSets();
			for (int i = 0; i < ts.size(); ++i)
				listModel.addElement(ts.get(i));
		}
		testSetList.setModel(listModel);
	}
	

	public void addWindow(VSVMInternalFrame fr) {		
		desktopPane.add(fr);
		fr.moveToFront();
		try { 
			fr.setMaximum(true);
		} catch (Exception ex) {
			
		}	
	}
	
	public void updateWindowMenu() {
		menu.updateWindows();
	}
	
	
	public LayoutableDesktopPane getDesktop() {
		return desktopPane;
	}
	
	public WorkspaceTree getObjectsTree() {
		return workspaceTree;
	}

}  //  @jve:decl-index=0:visual-constraint="10,10"
